package com.yj.change.adapter;

import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public abstract class ListAdapter<T> extends BaseAdapter {
    private List<T> infos;
    private LayoutInflater inflater;

    public ListAdapter() {
    }

    private void initInfos() {
        this.infos = this.infos == null ? new ArrayList<T>() : this.infos;
    }

    public void addInfo(T info) {
        this.initInfos();
        this.infos.add(info);
    }

    public void addInfo(int index, T info) {
        this.initInfos();
        this.infos.add(index, info);
    }

    public void removeInfo(T info) {
        this.initInfos();
        this.infos.remove(info);
    }

    public void removeIndex(int index) {
        this.initInfos();
        this.infos.remove(index);
    }

    public void addInfos(List<T> infos) {
        this.initInfos();
        this.infos.addAll(infos);
    }

    public void addInfos(int index, List<T> infos) {
        this.initInfos();
        this.infos.addAll(index, infos);
    }

    public void removeInfos(List<T> infos) {
        this.initInfos();
        this.infos.removeAll(infos);
    }

    public void clear() {
        this.initInfos();
        this.infos.clear();
    }

    public List<T> getInfos() {
        return this.infos;
    }

    public long getItemId(int position) {
        return (long) position;
    }

    public T getItem(int position) {
        return this.infos.get(position);
    }

    public int getCount() {
        return this.infos == null ? 0 : this.infos.size();
    }

    @Override
    public final View getView(int i, View view, ViewGroup viewGroup) {
        inflater = inflater == null ? inflater = LayoutInflater.from(viewGroup.getContext()) : inflater;
        Holder holder = null;
        if (view == null) {
            view = inflater.inflate(getLayout(), viewGroup, false);
            holder = new Holder(view);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        bindData(holder, i);
        return view;
    }

    protected abstract void bindData(Holder holder, int postion);

    protected abstract int getLayout();

    public class Holder {
        private final View itemView;
        private SparseArray<View> views = null;

        private Holder(View view) {
            this.itemView = view;
            views = new SparseArray<>();
        }

        public View getItemView() {
            return this.itemView;
        }

        public <V extends View> V findViewById(int id) {
            V v = (V) views.get(id);
            if (v != null) {
                return v;
            }
            v = itemView.findViewById(id);
            views.put(id, v);
            return v;
        }

        public TextView setText(int resId, String text) {
            TextView textView = findViewById(resId);
            textView.setText(text);
            return  textView;
        }

        public <V extends View> V setImageDrawable(int resId, Drawable draw) {
            ImageView imageView = findViewById(resId);
            if (draw != null)
                imageView.setImageDrawable(draw);
            return (V) imageView;
        }

    }
}
